package com.kangyi.user.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.kangyi.constant.MessageConstant;
import com.kangyi.dto.UserComplaintDTO;
import com.kangyi.dto.UserComplaintImgDTO;
import com.kangyi.entity.UserComplaint;
import com.kangyi.entity.UserComplaintImg;
import com.kangyi.exception.DataNotExistException;
import com.kangyi.user.mapper.UserComplaintImgMapper;
import com.kangyi.user.mapper.UserComplaintMapper;
import com.kangyi.user.service.UserComplaintService;
import com.kangyi.vo.UserComplaintVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Blank
 * @version 1.0
 */
@Service
public class UserComplaintServiceImpl implements UserComplaintService {

    @Autowired
    private UserComplaintMapper userComplaintMapper;

    @Autowired
    private UserComplaintImgMapper userComplaintImgMapper;

    @Override
    public void feedback(UserComplaintDTO userComplaintDTO) {

        UserComplaint userComplaint = new UserComplaint();
        BeanUtil.copyProperties(userComplaintDTO,userComplaint);
        userComplaint.setStatus(UserComplaint.PENDING);
        userComplaintMapper.feedback(userComplaint);

        Integer id = userComplaint.getId();

        if (id == null) {
            throw new RuntimeException("插入用户投诉信息后，ID 未正确回填");
        }

        List<UserComplaintImgDTO> userComplaintImgDTOList = userComplaintDTO.getUserComplaintImgDTOList();
        if (userComplaintImgDTOList != null && !CollUtil.isEmpty(userComplaintImgDTOList)) {
            for (UserComplaintImgDTO userComplaintImgDTO : userComplaintImgDTOList) {
                UserComplaintImg build = UserComplaintImg.builder()
                        .userComplaintId(id)
                        .image(userComplaintImgDTO.getImage())
                        .sort(userComplaintImgDTO.getSort())
                        .build();
                userComplaintImgMapper.insertImage(build);
            }
        }

    }

    @Override
    public List<UserComplaintVO> inquiryFeedBack(Integer userId) {

        //根据用户id查询反馈等信息
        List<UserComplaintVO> userComplaintVOList = userComplaintMapper.inquiryFeedBack(userId);
        //判断返回数据是否为空
        if (CollUtil.isEmpty(userComplaintVOList)) {
            throw new DataNotExistException(MessageConstant.DATA_NOT_EXIST);
        }
        //数据返回
        return userComplaintVOList;
    }
}
